package com.service;

import com.entity.Article;
import com.mapper.ArticleMapper;
import com.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.*;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class ArticleService implements ArticleMapper{
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    /**
     * 查询所有文章
     * @return 文章集合
     */
    public List<Article> findAllArticle() {
        SqlSession sqlSession = factory.openSession();
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);
        List<Article> articles = mapper.findAllArticle();
        sqlSession.close();
        return articles;
    }


    /**
     * 分页查询
     * @param pageNum 页码
     * @param offset 偏移量，也就是每页的页数
     * @return 文章集合
     */
    public List<Article> findActiclePage(int pageNum , int offset){
        int pageSize= (pageNum-1)*offset; //pageSize start条数
        SqlSession sqlSession = factory.openSession();
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);
        List<Article> articles = mapper.findActiclePage(pageSize,offset);
        sqlSession.close();
        return articles;
    }

    /**
     * 、根据文章ID查询文章
     * @param id 文章ID
     * @return 文章对象
     */
    @Override
    public Article findArticleById(int id) {
        SqlSession sqlSession = factory.openSession();
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);
        Article article = mapper.findArticleById(id);
        return article;
    }


    public List<Article> findArticleByName(String title){
        SqlSession sqlSession = factory.openSession();
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);
        List<Article> articlename = mapper.findArticleByName(title);
    //        System.out.println(articlename.get(0).getArt_title());
        return articlename;
}


    public int count(){
        SqlSession sqlSession = factory.openSession();
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);
        int count = mapper.count();
        return count;
    }

    public int addArticle (Article article) {
        SqlSession sqlSession = factory.openSession();
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);
        int rows = mapper.addArticle(article);
    System.out.println("更新" + rows + "条");
        sqlSession.commit();
        return rows;
    }

    @Override
    public int delArticleById(int id) {
        SqlSession sqlSession = factory.openSession();
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);
        int i = mapper.delArticleById(id);
        sqlSession.commit();
        return i;
    }


    @Override
    public int updateArticleById(String title, String context, String update_time, int id) {
        SqlSession sqlSession = factory.openSession();
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);
        int i = mapper.updateArticleById(title, context, update_time, id);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }



}
